from werkzeug.security import generate_password_hash, check_password_hash

from main.ext import db
from main.models import UserBaseModel, Base
from main.models.goods.goods_model import Goods

NOT_PAID = False
PAID = True
VALID = True
NOT_VALID = False


class UsersModel(UserBaseModel):
    __tablename__ = 'users'
    email = db.Column(db.String(40), nullable=False, unique=True)
    is_verify = db.Column(db.Boolean, default=False)


class CartModel(Base):
    __tablename__ = 'cart'
    c_user_id = db.Column(db.Integer, db.ForeignKey(UsersModel.id))
    c_goods_id = db.Column(db.Integer, db.ForeignKey(Goods.id))
    c_goods_count = db.Column(db.Integer, default=1)
    c_is_selected = db.Column(db.Boolean, default=True)
    goods = db.relationship('Goods', backref='cart')  # 用relationship使CartModel能快捷访问Goods里的信息


class OrderModel(Base):
    __tablename__ = 'order'
    o_user_id = db.Column(db.Integer, db.ForeignKey(UsersModel.id))
    o_price = db.Column(db.Float, default=0)
    o_time = db.Column(db.DateTime)
    o_status = db.Column(db.Boolean, default=NOT_PAID)
    o_valid = db.Column(db.Boolean, default=VALID)
    goods = db.relationship('OrderGoodsModel', backref='order')


class OrderGoodsModel(Base):
    __tablename__ = 'ordergoods'
    o_order_id = db.Column(db.Integer, db.ForeignKey(OrderModel.id))
    o_goods_id = db.Column(db.Integer, db.ForeignKey(Goods.id))
    o_goods_count = db.Column(db.Integer, default=1)
    goods = db.relationship('Goods', backref='ordergoods')
